import javax.swing.plaf.PanelUI;
import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;


public class SnakeHead  extends GameObj{
    private String direction = "right";
    public String getDirection() {return direction; }
    public void setDirection(String direction) {
        this.direction = direction;
    }
    public void changeDirection(KeyEvent keyEvent){
        switch (keyEvent.getKeyCode()){
            case KeyEvent.VK_W:
                direction = "up";
                img = GameUtils.upImg;
                break;
            case KeyEvent.VK_S:
                direction = "down";
                img = GameUtils.downImg;
                break;
            case KeyEvent.VK_A:
                direction = "left";
                img = GameUtils.leftImg;
                break;
            case KeyEvent.VK_D:
                direction = "right";
                img = GameUtils.rightImg;
                break;
            default:
                break;
        }
    }
    public void move(){
        switch (direction){
            case "right":
                x += frame.step;
                x = (x>= frame.frameSize) ? 0 : x;
                break;
            case "left":
                x -= frame.step;
                x = (x<=0) ? frame.frameSize : x;
                break;
            case "up":
                y -= frame.step;
                y = (y<=0) ? frame.frameSize : y;
                break;
            case "down":
                y += frame.step;
                y = (y>= frame.frameSize) ? 0: y;
                break;
        }
    }

    public SnakeHead(Image img, int x, int y, GameFrame frame) {
        super(img, x, y, frame);
        this.frame.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
               changeDirection(e);
            }
        });
    }
    @Override
    public void paintSelf(Graphics g){
        super.paintSelf(g);
        move();
    }
}
